package org.demo.patten.proxy;

/**
 * @author zhuj
 * @date 2025/3/29
 * @apiNote
 */
public class DatabaseProxy implements Database{

    private RealDatabase realDatabase;
    private String userRole;
    public DatabaseProxy(String userRole) {
        this.userRole = userRole;
    }
    @Override
    public void query(String sql) {
        if (!"ADMIN".equals(userRole)) {
            System.out.println("拒绝访问：权限不足");
            return;
        }
        // 延迟初始化
        if (realDatabase == null) {
            realDatabase = new RealDatabase();
        }
        // 添加日志
        System.out.println("日志：用户角色 [" + userRole + "] 执行查询");
        realDatabase.query(sql);
    }
}
